package com.schoolET.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.schoolET.entity.OrderManagement;
import com.schoolET.entity.dto.OrderManagementDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;

/**
 * <p>
 * 用户商品交易 Mapper 接口
 * </p>
 *
 * @author lin
 * @since 2024-06-01
 */
@Mapper
public interface OrderManagementMapper extends BaseMapper<OrderManagement> {

    @Select("select * from order_management where goods_id = #{goodsId} and flag = 0")
    OrderManagement selectByGoodsId(Integer goodsId);

    @Select("SELECT t1.product_status,t1.trading_status,t2.picture,t2.price,t2.theme,t2.goods_describe,t2.price,\n" +
            "t3.open_id AS seller_open_id,t5.open_id AS buyer_open_id,\n" +
            "t3.nick_name AS seller_nickname,t5.nick_name AS buyer_nickname,t1.buyer_address\n" +
            " FROM order_management t1\n" +
            "LEFT JOIN goods t2 ON t1.goods_id=t2.id\n" +
            "LEFT JOIN wechat t3 ON t1.release_id = t3.id\n" +
            "LEFT JOIN wechat t5 ON t1.buyer_id =t5.id\n" +
            "WHERE t1.trading_status = 0 ")
    Page<OrderManagementDTO> selectByproductStatus(Page<?> page);


    @Update("update order_management " +
            "set product_status = 1,update_time = #{date} " +
            "where " +
            "goods_id = #{goodsId} " +
            "and release_id = #{releaseId} " +
            "and trading_status = 1")
    Integer updateOrderManagementByGoodsIdAndReleaseId(@Param("goodsId")Integer goodsId,
                                                       @Param("releaseId")Integer releaseId,
                                                       @Param("date")Date date);


    @Update("update order_management " +
            "set trading_status = 0,buyer_id = #{buyerId},buyer_address = #{buyerAddress},update_time = #{date} " +
            "where goods_id = #{goodsId}")
    Integer updateOrderManagementByPurchase(@Param("goodsId")Integer goodsId,
                                            @Param("buyerId")Integer buyerId,
                                            @Param("buyerAddress") String buyerAddress,
                                            @Param("date") Date date);

    @Update("update order_management set trading_status = 2,update_time = #{date} where goods_id = #{goodsId} and buyer_id = #{buyerId}")
    Integer updateOrderManagementByConfirm(@Param("goodsId")Integer goodsId, @Param("buyerId")Integer buyerId,@Param("date")Date date);


    @Update("update order_management set trading_status = 1,update_time = #{date} where goods_id = #{goodsId} and buyer_id = #{buyerId}")
    Integer updateOrderManagementByStop(@Param("goodsId")Integer goodsId, @Param("buyerId")Integer buyerId,@Param("date")Date date);

}
